package com.zhouxiaoge.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

/***************************************************
 *                    _ooOoo_                  *   
 *                   o8888888o                 * 佛        
 *                  88'' . ''88                * 祖 
 *                  ( | -_- | )                * 保 
 *                   0\  =  /0                 * 佑 
 *                ____/'---'\____              *    
 *              .'  \\|     |//  '.            * 永 
 *             /  \\|||  :  |||//  \           * 无 
 *            /  _||||| -:- |||||-  \          * BUG
 *            |   | \\\  -  /// |   |          *
 *            | \_|  ''\---/''  |   |          * 阿
 *            \  .-\__  `-`  ___/-. /          * 弥
 *          ___`. .'  /--.--\  `. . __         * 陀
 *       ."" '<  ‘.___\_<|>_/___.’  >'"".      * 佛
 *      | | :  ` - `.;`\ _ /`;.`/ - ` : | |    *
 *      \  \ `-.   \_ __\ /__ _/   .-` /  /    * 
 * ======`-.____`-.___\_____/___.-`____.-'=====*
 * @author 周明阳 mail:gqzmy@outlook.com 时间:2018-01-09 16:12
 */
public class MongoDBJDBC {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
//        mongoDatabase.createCollection("zhouxiaogoe");

        MongoCollection<Document> zhouxiaogoe = mongoDatabase.getCollection("zhouxiaoge");


        Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100).append("by", "Fly");
        List<Document> documents = new ArrayList<>();
        documents.add(document);

        zhouxiaogoe.insertMany(documents);

        zhouxiaogoe.updateMany(Filters.eq("likes", 100), new Document("$set", new Document("likes", 222)));

//        zhouxiaogoe.deleteOne(Filters.eq("likes", 222));
        zhouxiaogoe.deleteMany(Filters.eq("likes", 200));


        FindIterable<Document> findIterable = zhouxiaogoe.find();

        for (Document aFindIterable : findIterable) {
            System.out.println(aFindIterable);
        }
    }
}
